package com.luo.backend.dto;

import com.luo.backend.entity.GroupBuyingActivity;
import com.luo.backend.entity.GroupBuyingParticipant;
import com.fasterxml.jackson.annotation.JsonFormat;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 团购活动响应DTO
 */
public class GroupBuyingResponse {
    
    private Long id;
    private Long initiatorUserId;
    private String initiatorUsername;
    private Long foodId;
    private String foodName;
    private String foodImage;
    private Long merchantId;
    private String merchantName;
    private String title;
    private String description;
    private Integer minParticipants;
    private Integer maxParticipants;
    private Integer currentParticipants;
    private BigDecimal originalPrice;
    private BigDecimal groupPrice;
    private BigDecimal savedAmount; // 节省金额
    private BigDecimal deliveryFee; // 配送费
    
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
    private LocalDateTime startTime;
    
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
    private LocalDateTime endTime;
    
    private String status;
    private String statusText;
    private Boolean isExpired; // 是否已过期
    private Boolean canJoin; // 是否可以参与
    private Boolean hasJoined; // 当前用户是否已参与
    
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
    private LocalDateTime createdAt;
    
    private List<ParticipantResponse> participants;
    
    // 内部类 - 参与者响应
    public static class ParticipantResponse {
        private Long id;
        private Long userId;
        private String username;
        private String realName; // ⭐ 真实姓名（优先显示）
        private String avatar; // ⭐ 用户头像URL
        private Integer quantity;
        private String spiceLevel;
        private String status;
        private String statusText;
        
        @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
        private LocalDateTime joinedAt;
        
        public ParticipantResponse(GroupBuyingParticipant participant) {
            this.id = participant.getId();
            this.userId = participant.getUserId();
            this.quantity = participant.getQuantity();
            this.spiceLevel = participant.getSpiceLevel();
            this.status = participant.getStatus();
            this.statusText = getStatusText(participant.getStatus());
            this.joinedAt = participant.getJoinedAt();
        }
        
        private String getStatusText(String status) {
            switch (status) {
                case "joined": return "已参与";
                case "paid": return "已支付";
                case "cancelled": return "已取消";
                default: return "未知状态";
            }
        }
        
        // Getters and Setters
        public Long getId() { return id; }
        public void setId(Long id) { this.id = id; }
        
        public Long getUserId() { return userId; }
        public void setUserId(Long userId) { this.userId = userId; }
        
        public String getUsername() { return username; }
        public void setUsername(String username) { this.username = username; }
        
        public String getRealName() { return realName; }
        public void setRealName(String realName) { this.realName = realName; }
        
        public String getAvatar() { return avatar; }
        public void setAvatar(String avatar) { this.avatar = avatar; }
        
        public Integer getQuantity() { return quantity; }
        public void setQuantity(Integer quantity) { this.quantity = quantity; }
        
        public String getSpiceLevel() { return spiceLevel; }
        public void setSpiceLevel(String spiceLevel) { this.spiceLevel = spiceLevel; }
        
        public String getStatus() { return status; }
        public void setStatus(String status) { this.status = status; }
        
        public String getStatusText() { return statusText; }
        public void setStatusText(String statusText) { this.statusText = statusText; }
        
        public LocalDateTime getJoinedAt() { return joinedAt; }
        public void setJoinedAt(LocalDateTime joinedAt) { this.joinedAt = joinedAt; }
    }
    
    // 构造函数
    public GroupBuyingResponse() {}
    
    public GroupBuyingResponse(GroupBuyingActivity activity) {
        this.id = activity.getId();
        this.initiatorUserId = activity.getInitiatorUserId();
        this.foodId = activity.getFoodId();
        this.merchantId = activity.getMerchantId();
        this.title = activity.getTitle();
        this.description = activity.getDescription();
        this.minParticipants = activity.getMinParticipants();
        this.maxParticipants = activity.getMaxParticipants();
        this.currentParticipants = activity.getCurrentParticipants();
        this.originalPrice = activity.getOriginalPrice();
        this.groupPrice = activity.getGroupPrice();
        this.savedAmount = activity.getOriginalPrice().subtract(activity.getGroupPrice());
        this.startTime = activity.getStartTime();
        this.endTime = activity.getEndTime();
        this.status = activity.getStatus();
        this.statusText = getStatusText(activity.getStatus());
        this.isExpired = LocalDateTime.now().isAfter(activity.getEndTime());
        this.canJoin = "active".equals(activity.getStatus()) && 
                      activity.getCurrentParticipants() < activity.getMaxParticipants() &&
                      !this.isExpired;
        this.createdAt = activity.getCreatedAt();
    }
    
    private String getStatusText(String status) {
        switch (status) {
            case "active": return "进行中";
            case "success": return "拼团成功";
            case "failed": return "拼团失败";
            case "cancelled": return "已取消";
            default: return "未知状态";
        }
    }
    
    // Getters and Setters
    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }
    
    public Long getInitiatorUserId() { return initiatorUserId; }
    public void setInitiatorUserId(Long initiatorUserId) { this.initiatorUserId = initiatorUserId; }
    
    public String getInitiatorUsername() { return initiatorUsername; }
    public void setInitiatorUsername(String initiatorUsername) { this.initiatorUsername = initiatorUsername; }
    
    public Long getFoodId() { return foodId; }
    public void setFoodId(Long foodId) { this.foodId = foodId; }
    
    public String getFoodName() { return foodName; }
    public void setFoodName(String foodName) { this.foodName = foodName; }
    
    public String getFoodImage() { return foodImage; }
    public void setFoodImage(String foodImage) { this.foodImage = foodImage; }
    
    public Long getMerchantId() { return merchantId; }
    public void setMerchantId(Long merchantId) { this.merchantId = merchantId; }
    
    public String getMerchantName() { return merchantName; }
    public void setMerchantName(String merchantName) { this.merchantName = merchantName; }
    
    public String getTitle() { return title; }
    public void setTitle(String title) { this.title = title; }
    
    public String getDescription() { return description; }
    public void setDescription(String description) { this.description = description; }
    
    public Integer getMinParticipants() { return minParticipants; }
    public void setMinParticipants(Integer minParticipants) { this.minParticipants = minParticipants; }
    
    public Integer getMaxParticipants() { return maxParticipants; }
    public void setMaxParticipants(Integer maxParticipants) { this.maxParticipants = maxParticipants; }
    
    public Integer getCurrentParticipants() { return currentParticipants; }
    public void setCurrentParticipants(Integer currentParticipants) { this.currentParticipants = currentParticipants; }
    
    public BigDecimal getOriginalPrice() { return originalPrice; }
    public void setOriginalPrice(BigDecimal originalPrice) { this.originalPrice = originalPrice; }
    
    public BigDecimal getGroupPrice() { return groupPrice; }
    public void setGroupPrice(BigDecimal groupPrice) { this.groupPrice = groupPrice; }
    
    public BigDecimal getSavedAmount() { return savedAmount; }
    public void setSavedAmount(BigDecimal savedAmount) { this.savedAmount = savedAmount; }
    
    public BigDecimal getDeliveryFee() { return deliveryFee; }
    public void setDeliveryFee(BigDecimal deliveryFee) { this.deliveryFee = deliveryFee; }
    
    public LocalDateTime getStartTime() { return startTime; }
    public void setStartTime(LocalDateTime startTime) { this.startTime = startTime; }
    
    public LocalDateTime getEndTime() { return endTime; }
    public void setEndTime(LocalDateTime endTime) { this.endTime = endTime; }
    
    public String getStatus() { return status; }
    public void setStatus(String status) { this.status = status; }
    
    public String getStatusText() { return statusText; }
    public void setStatusText(String statusText) { this.statusText = statusText; }
    
    public Boolean getIsExpired() { return isExpired; }
    public void setIsExpired(Boolean isExpired) { this.isExpired = isExpired; }
    
    public Boolean getCanJoin() { return canJoin; }
    public void setCanJoin(Boolean canJoin) { this.canJoin = canJoin; }
    
    public Boolean getHasJoined() { return hasJoined; }
    public void setHasJoined(Boolean hasJoined) { this.hasJoined = hasJoined; }
    
    public LocalDateTime getCreatedAt() { return createdAt; }
    public void setCreatedAt(LocalDateTime createdAt) { this.createdAt = createdAt; }
    
    public List<ParticipantResponse> getParticipants() { return participants; }
    public void setParticipants(List<ParticipantResponse> participants) { this.participants = participants; }
}

